package com.mm.cloth.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mm.cloth.R;
import com.mm.cloth.model.TaskStatus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class TaskStatusAdapter extends RecyclerView.Adapter<TaskStatusAdapter.TaskViewHolder> {
    private final Context context;
    private final List<TaskStatus> tasks;
    private final SimpleDateFormat dateFormat;
    
    private OnTaskClickListener onTaskClickListener;
    private OnTaskDeleteListener onTaskDeleteListener;
    
    public interface OnTaskClickListener {
        void onTaskClick(TaskStatus task);
    }
    
    public interface OnTaskDeleteListener {
        void onTaskDelete(TaskStatus task);
    }
    
    public TaskStatusAdapter(Context context) {
        this.context = context;
        this.tasks = new ArrayList<>();
        this.dateFormat = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
    }
    
    public void setOnTaskClickListener(OnTaskClickListener listener) {
        this.onTaskClickListener = listener;
    }
    
    public void setOnTaskDeleteListener(OnTaskDeleteListener listener) {
        this.onTaskDeleteListener = listener;
    }
    
    public void updateTasks(List<TaskStatus> newTasks) {
        tasks.clear();
        tasks.addAll(newTasks);
        notifyDataSetChanged();
    }
    
    public void notifyTaskUpdated(TaskStatus task) {
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i).getTaskId().equals(task.getTaskId())) {
                notifyItemChanged(i);
                break;
            }
        }
    }
    
    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_status, parent, false);
        return new TaskViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        TaskStatus task = tasks.get(position);
        
        // 设置任务类型和图标
        holder.tvTaskType.setText(task.getTaskType().getDisplayName());
        setTaskIcon(holder.ivTaskIcon, task.getTaskType());
        
        // 设置状态
        holder.tvStatus.setText(task.getStatus().getDisplayName());
        holder.tvStatus.setTextColor(Color.parseColor(task.getStatus().getColor()));
        
        // 设置时间
        holder.tvTime.setText(dateFormat.format(task.getCreateTime()));
        
        // 设置描述
        holder.tvDescription.setText(task.getDescription());
        
        // 设置进度
        if (task.getStatus() == TaskStatus.Status.PROCESSING) {
            holder.progressBar.setVisibility(View.VISIBLE);
            holder.tvProgress.setVisibility(View.VISIBLE);
            holder.progressBar.setProgress(task.getProgress());
            holder.tvProgress.setText(task.getProgress() + "%");
        } else {
            holder.progressBar.setVisibility(View.GONE);
            holder.tvProgress.setVisibility(View.GONE);
        }
        
        // 设置状态图标
        setStatusIcon(holder.ivStatusIcon, task.getStatus());
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onTaskClickListener != null) {
                onTaskClickListener.onTaskClick(task);
            }
        });
        
        // 设置删除按钮
        holder.btnDelete.setOnClickListener(v -> {
            if (onTaskDeleteListener != null) {
                onTaskDeleteListener.onTaskDelete(task);
            }
        });
        
        // 根据状态显示/隐藏删除按钮
        if (task.getStatus() == TaskStatus.Status.PROCESSING || 
            task.getStatus() == TaskStatus.Status.PENDING) {
            holder.btnDelete.setVisibility(View.GONE);
        } else {
            holder.btnDelete.setVisibility(View.VISIBLE);
        }
    }
    
    private void setTaskIcon(ImageView imageView, TaskStatus.TaskType taskType) {
        int iconRes;
        switch (taskType) {
            case MATTING:
                iconRes = R.drawable.ic_matting;
                break;
            case FACE_SWAP:
                iconRes = R.drawable.ic_face_swap;
                break;
            case CLOTHES_SWAP:
                iconRes = R.drawable.ic_clothes_swap;
                break;
            case PHOTO_ENHANCE:
                iconRes = R.drawable.ic_photo_enhance;
                break;
            case AI_PAINTING:
                iconRes = R.drawable.ic_ai_painting;
                break;
            case PHOTO_RESTORE:
                iconRes = R.drawable.ic_photo_restore;
                break;
            case ID_PHOTO:
                iconRes = R.drawable.ic_id_photo;
                break;
            default:
                iconRes = R.drawable.ic_tools;
                break;
        }
        imageView.setImageResource(iconRes);
    }
    
    private void setStatusIcon(ImageView imageView, TaskStatus.Status status) {
        int iconRes;
        switch (status) {
            case PENDING:
                iconRes = R.drawable.ic_pending;
                break;
            case PROCESSING:
                iconRes = R.drawable.ic_processing;
                break;
            case COMPLETED:
                iconRes = R.drawable.ic_completed;
                break;
            case FAILED:
                iconRes = R.drawable.ic_failed;
                break;
            default:
                iconRes = R.drawable.ic_pending;
                break;
        }
        imageView.setImageResource(iconRes);
    }
    
    @Override
    public int getItemCount() {
        return tasks.size();
    }
    
    static class TaskViewHolder extends RecyclerView.ViewHolder {
        ImageView ivTaskIcon;
        TextView tvTaskType;
        TextView tvStatus;
        TextView tvTime;
        TextView tvDescription;
        TextView tvProgress;
        ProgressBar progressBar;
        ImageView ivStatusIcon;
        ImageView btnDelete;
        
        TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            ivTaskIcon = itemView.findViewById(R.id.ivTaskIcon);
            tvTaskType = itemView.findViewById(R.id.tvTaskType);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvDescription = itemView.findViewById(R.id.tvDescription);
            tvProgress = itemView.findViewById(R.id.tvProgress);
            progressBar = itemView.findViewById(R.id.progressBar);
            ivStatusIcon = itemView.findViewById(R.id.ivStatusIcon);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }
}
